package practice1_100;

import java.util.Stack;

public class Practice150 {
    public int evalRPN(String[] tokens) {
        //用栈的思想来解决这个问题
        if(tokens.length==0) return 0;
        int res=0;
        Stack<Integer> stack=new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if(!(tokens[i].equals("+")||tokens[i].equals("-")||tokens[i].equals("*")||tokens[i].equals("/"))){
                stack.push(Integer.parseInt(tokens[i]));
            }else {
                int next=stack.pop();
                int first=stack.pop();
                if(tokens[i].equals("+")){
                    stack.push(next+first) ;
                }else if(tokens[i].equals("-")){
                    stack.push(first-next);
                }else if(tokens[i].equals("*")){
                    stack.push(first*next);
                }else {
                    stack.push(first/next);
                }
            }
        }
        return stack.pop();
    }
}
